﻿/************************************************************************
 * AIDE/Examples - Automata Identification Engine
 * Copyright (C) 2012-2013 Ali Khalili (khalili_DOT_ir_@SIGN_gmail_DOT_com)
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License (LGPL) published 
 * by the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version. This program is distributed without any
 * warranty. See the GNU Lesser General Public License for more details. 
 * ***********************************************************************
 * File         :     
 * Description  :     
 * Change log   :     	-Date:  Change
 ************************************************************************/

//#define USE_GLEE_VISUALISATION

using System;
using System.Collections.Generic;
using SMLib.Automata.MM;
using AIDE.Base;
using AIDE.Oracles.NFMs;
using AIDE.OnlineAlgo.NFMs;

namespace LearningApp.Examples
{
    /// <summary>
    /// An example to show how to (i) generate a mealy machine (ii) 
    /// use it as emulator (iii) learn it (iv) export it as dot file
    /// </summary>
    public class RandomMealy
    {
        public static void RunExample()
        {
            //* size of automata (number of states in Mealy machine)
            int stateSize = 10;
            //* Number of states having nondeterministic transitions
            int BranchPoints = 2;
            //* Degree of nondeterminism in states that have nondeterministic transitions
            int BranchDegree = 2;
            //* size of input alphabet
            int SigmaILen = 5;
            //* size of output alphabet
            int SigmaOLen = 5;
            //* name of machine
            string machineName = "sampleMachine";

            //* generate a mealy machine
            MealyMachine originalMM = MM_Generator.GenerateARandomMachine(machineName, stateSize, BranchPoints, BranchDegree, SigmaILen, SigmaOLen);
            
            //* export it into a dot file
            SMLib.Interoperability.DOT.DotHelper_MM.WriteToDot("original.dot", originalMM);
            
            Console.WriteLine("With the given configuration, the generared machine is exported as " +
                 "original.dot");
            Console.WriteLine("Press any key to start learning it...");
            Console.ReadKey();

            //* learn the generated machine
            SUL_Emulator_MM emulator = new SUL_Emulator_MM(originalMM);
            Oracle_NMAT_Realistic oracle = new Oracle_NMAT_Realistic(emulator);
            N_Star learner = new N_Star(oracle);
            MealyMachine learntMM = learner.Learn();

            Console.WriteLine("The generated machine as a black box system is leart.");
#if USE_GLEE_VISUALISATION
            //* visualize the learnt machine
            Visualization.SSViewer<MealyMachine.Mealy_State, MealyMachine.Mealy_Transition> viewer =
                new Visualization.SSViewer<MealyMachine.Mealy_State, MealyMachine.Mealy_Transition>(learntMM);
            viewer.ShowDialog();
#endif
            //* and export it
            SMLib.Interoperability.DOT.DotHelper_MM.WriteToDot( "learnt.dot", learntMM);
            Console.WriteLine("The learnt machine is exported as learnt.dot");
        }
    }
}
